package hesso.smartdraw.ch;

import hesso.smartdraw.ch.db.DBHelper;
import hesso.smartdraw.ch.db.SyncDbAndDb;
import hesso.smartdraw.ch.model.Building;
import hesso.smartdraw.ch.model.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditBuildingScreen extends Activity{

	
	private int userId;
	private int buildingId;
	private Building building;
	private TextView buildingLevels;
	private TextView buildingRooms;
	private EditText buildingName;
	private DBHelper dbHelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.editbuildingscreen);
        
        buildingLevels = (TextView) findViewById(R.id.textBuildingLevels);
        buildingRooms = (TextView) findViewById(R.id.textBuildingRooms);
        buildingName = (EditText) findViewById(R.id.editTextBuildingName);
        
        dbHelper = DBHelper.getInstance(this);
        
        final Bundle extras = getIntent().getExtras();
    	this.userId = extras.getInt("userId");
    	this.buildingId = extras.getInt("buildingId");
    	
    	if(buildingId == -1) {
    		 building = new Building();
    		 building.addGroundLevel();
    	} else {
    		building = dbHelper.getBuildingByBuildingId(buildingId);
    		buildingName.setText(building.getName());
    		buildingLevels.setText("Levels: " + building.getLevelArray().size());
    		int roomCount = 0;
    		for(int i = 0; i < building.getLevelArray().size(); i++) {
    			for(int j = 0; j < building.getLevelArray().get(i).getRoomArray().size(); j++) {
    				roomCount++;
    			}
    		}
    		buildingRooms.setText("Rooms: " + roomCount);
    	}
    	
    	final Button buttonSave = (Button) findViewById(R.id.buttonSaveBuilding);
    	buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	saveBuilding();
            }
        });
        
        final Button buttonDelete = (Button) findViewById(R.id.buttonDeleteBuilding);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	deleteBuilding();
            }
        });
        
        final Button buttonSync = (Button) findViewById(R.id.buttonSyncBuilding);
        buttonSync.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	syncBuilding();
            }
        });
    }
    
    public void saveBuilding() {
    	
    	building.setName(buildingName.getText().toString());
    	User tempUser = new User(userId, "temp");
    	dbHelper.saveBuilding(building, tempUser);
    	Intent intent = new Intent(this,BuildingScreen.class);
    	intent.putExtra("userId", userId);
    	startActivity(intent);
    }
    
    public void deleteBuilding() {
    	
    	dbHelper.deleteBuildingByBuildingId(building.getId());
    	Intent intent = new Intent(this,BuildingScreen.class);
    	intent.putExtra("userId", userId);
    	startActivity(intent);
    }
    
    public void syncBuilding() {
    	
    	SyncDbAndDb syncDb = SyncDbAndDb.getInstance();
    	syncDb.sync(buildingId);
    	Intent intent = new Intent(this,EditBuildingScreen.class);
    	intent.putExtra("userId", userId);
    	intent.putExtra("buildingId", buildingId);
    	startActivity(intent);
    }
}
